﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Web.Mvc;
using Kaiser.Models;
using Kaiser.Modules.Cubic.DomainModel.News;
using Kaiser.Modules.Cubic.DomainModel.Product;
using Kaiser.Modules.Cubic.Repositories;
using Kaiser.Modules.Cubic.ViewModel;

namespace Kaiser.Modules.Cubic.Services.Impl
{
        [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class CubicService : ICubicService
    {

        private readonly IProductRepositorie _productRepositorie;
        private INewsRepositorie _newsRepositorie;
            private IProductCategoryRepositorie _IProductCategoryRepositorie;

            public CubicService()
        {
            _productRepositorie = DependencyResolver.Current.GetService<IProductRepositorie>();
            _newsRepositorie = DependencyResolver.Current.GetService<INewsRepositorie>();
            _IProductCategoryRepositorie = DependencyResolver.Current.GetService<IProductCategoryRepositorie>();
        
        }
        public CubicService(IProductRepositorie productRepositorie)
        {
            _productRepositorie = productRepositorie;
        }

        public DomainModel.Product.ProductDetail GetProduct(string id)
        {
            // var model = new ProductDetail() {};
            // model.ProductId = Guid.NewGuid().ToString();
            //model.ProductName = "Test111";

            //return  new ProductDetail(){};
            try
            {
                return _productRepositorie.GetProduct(id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<DomainModel.Product.ProductDetail> GetProductsByCategoryCode(string categoryCode)
        {
            try
            {
                return _productRepositorie.GetProductsByCategoryCode(categoryCode);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }



        private void AddTreeNodes(ProductCategory item, IList<ProductCategory> list)
        {
            var childrenList = list.Where(p => item.Id != -1 && item.Id == p.ParentId);
            if (childrenList.Count() < 0)
            {
                return;
            }
            foreach (var category in childrenList)
            {
                AddTreeNodes(category, list);
                item.Categories.Add(category);
            }
        }

        public IList<DomainModel.Product.ProductCategory> GetProductCateogrys(string categoryParentId)
        {
            try
            {
                try
                {
                    var productCategoryRepositorie =
                        DependencyResolver.Current.GetService<IProductCategoryRepositorie>();
                    var list = productCategoryRepositorie.SearchProductCategory(new ProductCategorySearch(), new PagingInfo(0, 100000));
                    IList<DomainModel.Product.ProductCategory> resultlist = new List<ProductCategory>();
                  
                  
                        var rows = list.rows.Where(x => x.ParentId == Convert.ToInt32(categoryParentId));
                    
                    foreach (var item in rows)
                    {
                        AddTreeNodes(item, list.rows);
                        resultlist.Add(item);
                    }
                    return resultlist;
                }
                catch (Exception)
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DomainModel.Product.ProductCategory GetProductCategory(string categoryId)
        {
            try
            {
                return _productRepositorie.GetProductCategory(categoryId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<DomainModel.News.NewsDetail> GetNewsByCategoryId(string categoryId)
        {
            try
            {
                return _productRepositorie.GetNewsByCategoryCode(categoryId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public string AddMessage(DomainModel.Board.Message msg)
        {
            try
            {
                return _productRepositorie.AddMessage(msg);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public DomainModel.News.NewsDetail GetNews(string newsId)
        {
            try
            {
                return _newsRepositorie.GetNews(newsId);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public DomainModel.News.NewsDetail GetNewsByCode(string newsCode)
        {
            try
            {
                return _newsRepositorie.GetNewsByCode(newsCode);
            }
            catch (Exception)
            {
                
                throw;
            }
        }


        public ProductCategory GetProductCategoryByCode(string categoryCode)
        {
            try
            {
                return _IProductCategoryRepositorie.GetProductCategoryByCode(categoryCode);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public DomainModel.News.NewsCategory GetNewsCategoryByCode(string categoryCode)
        {
            try
            {
                return _newsRepositorie.GetNewsCategoryByCode(categoryCode);
            }
            catch (Exception)
            {

                throw;
            }
        }


        public DomainModel.Advert.AdvertSpace GetAdvertSpaceByCode(string code)
        {
            try
            {
                return RepositoriesFactory.CreateIAdvertSpaceRepositorie().GetAdvertSpaceByCode(code);
            }
            catch (Exception)
            {
                throw;
            }
           
        }


        public IList<NewsCategory> GetNewsCategorysByParentCode(string categoryCode)
        {
            try
            {

                var obj = RepositoriesFactory.CreateINewsRepositorie().GetAll();

                var list = new List<NewsCategory>();
                var children = obj.Where(p => p.CategoryCode==categoryCode).ToList();
                if (children.Count > 0)
                {
                    foreach (var item in children)
                    {

                        AddTreeNodes(item, obj, Convert.ToInt32(item.Id));

                        list.Add(item);
                    }
                }
                return list;
            }
            catch (Exception)
            {
                throw;
            }
        }


       private  void AddTreeNodes(NewsCategory perantNodes, IList<NewsCategory> list, int parendId)
        {
            var childrenList = list.Where(p => parendId != 0 && p.ParentId == parendId).ToList();
            if (childrenList.Count <= 0) return;

            perantNodes.children = new List<NewsCategory>();
            foreach (var item in childrenList)
            {

                AddTreeNodes(item, list, item.Id);
                perantNodes.children.Add(item);
            }
        }
    }
}
